<template>
  <!-- 登录表单 -->
  <el-form 
    ref="loginFormRef" 
    :model="loginForm" 
    :rules="loginRules" 
    label-position="top" 
    class="login-form"
  >
    <el-form-item label="用户名" prop="username">
      <el-input 
        v-model="loginForm.username" 
        placeholder="请输入用户名" 
        :prefix-icon="UserFilled"
      ></el-input>
    </el-form-item>
    
    <el-form-item label="密码" prop="password">
      <el-input 
        v-model="loginForm.password" 
        type="password" 
        placeholder="请输入密码" 
        :prefix-icon="Lock"
        show-password
      ></el-input>
    </el-form-item>
    
    <el-form-item label="用户身份" prop="role">
      <el-select 
        v-model="loginForm.role" 
        placeholder="请选择用户身份" 
        class="role-select"
      >
        <el-option 
          v-for="role in loginRoleOptions" 
          :key="role.value" 
          :label="role.label" 
          :value="role.value"
        >
          <div class="role-option">
            <el-icon><component :is="role.icon" /></el-icon>
            <span>{{ role.label }}</span>
          </div>
        </el-option>
      </el-select>
    </el-form-item>
    
    <el-form-item>
      <el-button 
        type="primary" 
        class="login-button" 
        @click="handleLogin" 
        :loading="loading"
      >登录</el-button>
    </el-form-item>
    
    <div class="form-footer">
      <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
      <a href="javascript:void(0)" @click="forgotPassword">忘记密码?</a>
    </div>
  </el-form>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { UserFilled, Lock, Setting, Van } from '@element-plus/icons-vue'
import { setUserRole, type UserRole } from '@/router'

const router = useRouter()
const loading = ref(false)

// 登录角色选项
const loginRoleOptions = [
  { label: '管理员', value: 'admin' as UserRole, icon: Setting },
  { label: '客户', value: 'customer' as UserRole, icon: UserFilled },
  { label: '配送员', value: 'courier' as UserRole, icon: Van }
]

// 登录表单
const loginForm = reactive({
  username: '',
  password: '',
  role: 'customer' as UserRole, // 默认为客户角色
  remember: false
})

// 登录表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择用户身份', trigger: 'change' }
  ]
}

// 登录表单引用
const loginFormRef = ref(null)

// 处理登录
const handleLogin = () => {
  loginFormRef.value?.validate((valid) => {
    if (valid) {
      loading.value = true
      
      // 模拟登录请求
      setTimeout(() => {
        loading.value = false
        
        // 保存用户登录状态和角色
        localStorage.setItem('isLoggedIn', 'true')
        localStorage.setItem('user_role', loginForm.role)
        localStorage.setItem('username', loginForm.username)
        
        setUserRole(loginForm.role)
        
        ElMessage.success('登录成功')
        
        // 根据角色导航到对应页面
        if (loginForm.role === 'admin') {
          router.push('/admin')
        } else if (loginForm.role === 'customer') {
          router.push('/customer')
        } else if (loginForm.role === 'courier') {
          router.push('/courier')
        }
      }, 1000)
    } else {
      return false
    }
  })
}

// 忘记密码
const forgotPassword = () => {
  ElMessage.info('重置密码功能暂未开放，请联系管理员')
}
</script>

<style scoped>
.login-form {
  margin-top: 10px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  padding-bottom: 6px;
}

:deep(.el-input__inner) {
  height: 42px;
  border-radius: 6px;
  border-color: #dcdfe6;
}

:deep(.el-input__inner:focus) {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

:deep(.el-checkbox__inner) {
  border-radius: 4px;
}

.role-select {
  width: 100%;
}

.role-option {
  display: flex;
  align-items: center;
}

.role-option .el-icon {
  margin-right: 8px;
}

.login-button {
  width: 100%;
  margin-top: 10px;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 6px;
  background: linear-gradient(90deg, #409eff 0%, #2d78c3 100%);
  border: none;
  box-shadow: 0 6px 12px rgba(64, 158, 255, 0.2);
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(64, 158, 255, 0.3);
}

.form-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
}

.form-footer a {
  color: #409EFF;
  text-decoration: none;
}
</style> 